from abc import abstractmethod
from typing import List

from sam.util.esUtil import EsConfig, ES
from sam.util.fileUtil2 import write_file_quick


class Es2File(ES):

    def __init__(self, es_config: EsConfig, export_file_name: str = "从es中导出"):
        super().__init__(es_config=es_config)
        self.export_file_name = export_file_name

    @abstractmethod
    def handle_export_data_list(self, item_list: List[dict], scroll_index: int, title: list) -> list:
        pass

    def export_file(self, item_list: List[dict], scroll_index: int, title: list):
        line_list = self.handle_export_data_list(item_list, scroll_index, title)
        write_file_quick(line_list, export_file_name=self.export_file_name, optional="append", file_type="csv")

    def export_es_data_2_file(self, query_body: dict):
        self.query_by_scroll(query_body=query_body, handle_func=self.export_file)

    def export_es_data_2_file_by_query(self, query_body: dict):
        self.query_by_search(query_body=query_body, handle_func=self.export_file)


class CheckEsExists(object):

    def __init__(self, es_config: EsConfig):
        super().__init__()
        self.es = ES(es_config)
